package khtn.mylocation;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;

public class GoogleMapActivity extends MapActivity {

	MapView mapView;
	MapController mapController;
	GeoPoint geoPoint;
	MyLocationOverlay myLocationOverlay;
	
	@Override
	protected boolean isRouteDisplayed() {
	    return false;
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.googlemap);
		
		mapView = (MapView) findViewById(R.id.mapview);
		mapController = mapView.getController();
		myLocationOverlay = new MyLocationOverlay(this, mapView);
		
	    mapView.setBuiltInZoomControls(true);
		
		
		ImageButton btnLocation = (ImageButton)this.findViewById(R.id.btnLocation);
		btnLocation.setOnClickListener(onClickLocationButton);
		
		ImageButton btnInputData = (ImageButton)this.findViewById(R.id.btnInput);
		btnInputData.setOnClickListener(onClickInputDataButton);
		
	    String coordinates[] = {"10.768108", "106.654388"};
        double lat = Double.parseDouble(coordinates[0]);
        double lng = Double.parseDouble(coordinates[1]);
        
        geoPoint = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
     
      /*  
        Drawable drawable = this.getResources().getDrawable(R.drawable.marker);
        PointItemizedOverlay itemizedoverlay = new PointItemizedOverlay(drawable, this);
        
        OverlayItem overlayitem = new OverlayItem(geoPoint, "title", "this is a description");
        
        itemizedoverlay.addOverlay(overlayitem);
        mapView.getOverlays().add(itemizedoverlay);
        */
        
        mapController.animateTo(geoPoint);
        
        int maxZoom = mapView.getMaxZoomLevel();
        Log.i("info", "Max zoom: " + maxZoom);
        int initZoom = maxZoom - 5;
        
        mapController.setZoom(initZoom);
        
        

        // create an overlay that shows our current location
        myLocationOverlay.enableCompass();
        myLocationOverlay.enableMyLocation();
        myLocationOverlay.runOnFirstFix(new Runnable() { public void run() {
        	GeoPoint geoPoint = myLocationOverlay.getMyLocation();
        	if(geoPoint != null) {
        		mapController.animateTo(myLocationOverlay.getMyLocation());
        	}
        }});

        //add this overlay to the MapView and refresh it
       	mapView.getOverlays().add(myLocationOverlay);
       	
        mapView.invalidate();        
	}
	
	private View.OnClickListener onClickInputDataButton = new View.OnClickListener() {
    	public void onClick(View view) {
             Bundle bundle = new Bundle();
             bundle.putDouble("lng", myLocationOverlay.getMyLocation().getLongitudeE6() / 1E6);
             bundle.putDouble("lat", myLocationOverlay.getMyLocation().getLatitudeE6() / 1E6);
             
             Intent formActivity = new Intent(view.getContext(), LocationFormActivity.class);
             formActivity.putExtras(bundle);
             startActivityForResult(formActivity, 0);
		}
	};
	
	private View.OnClickListener onClickLocationButton = new View.OnClickListener() {
    	public void onClick(View v) {
    		GeoPoint geoPoint = myLocationOverlay.getMyLocation();
        	if(geoPoint != null) {
        		mapController.animateTo(myLocationOverlay.getMyLocation());
        	}
            mapView.invalidate();
		}
	};
	
	@Override
	protected void onResume() {
	    super.onResume();
	    // when our activity resumes, we want to register for location updates
	    myLocationOverlay.enableMyLocation();
	}

	@Override
	protected void onPause() {
	    super.onPause();
	    // when our activity pauses, we want to remove listening for location updates
	    myLocationOverlay.disableMyLocation();
	}
}
